

//加载方法
$(function (){
    $.ajax({
        url: "/DHMProceduring/queryByCheckTag01",
        type:"get",
        success:function (data){
            $("#zongshu").html(data);
        }
    })

    //模糊查询的方法，生成表格
    $("#scdjTable").bootstrapTable({
        method:"get",
        contentType:"application/json;charset=utf-8",
        url: "/DHMProceduring/findAllTag01",
        striped: true,
        pageNumber:1,
        pagination:true,
        sidePagination:"server",//设置服务端分页还是客户端分页
        pageSize:5,//页大小
        pageList:[3,5,9],//每页显示的条数
        queryParams:function (params) {
            return {
                pageNum: (params.offset / params.limit) + 1,
                pageSize:params.limit,
            }
        },
        columns:[
            {title:'生产派工单编号',field:'manufactureId',align:'center'},
            {title:'产品编号',field:'productId',align:'center'},
            {title:'产品名称',field:'productName',align:'center'},
            {title:'数量',field:'amount',align:'center'},
            {title:'生产计划序号组',field:'applyIdGroup',align:'center'},
            {title:'登记时间',field:'registerTime',align:'center'},
            {title:'登记',field:'id',align:'center',
                formatter:function (value,row,index) {
                    let btn="<button class='btn btn-info' onclick='registerBtn("+value+")'>登记</button>";
                    return btn;
                }
            },
        ]
    })

})

//点击登记
let mMId=0;
function registerBtn(id){
    mMId=id;
    //进入单页
    $(".div1").hide();
    $(".div2").show();
    $("#register").html("");
    $("#productId").html('');
    $("#productName").html("");
    $("#productDescribe").html("");
    $("#amount").html("");
    $("#remark").html("");
    $("#checkTime").html("");
    $("#registerTime").html("");
    $("#realModuleCostPriceSum").html("");
    $("#moduleCostPriceSum").html("");
    $("#checker").html('');
    $("#realLabourCostPriceSum").html("");
    $("#labourCostPriceSum").html("");
    $("#manufactureId").html("");
    //查询产品数量和信息
    $.ajax({
        url:"/DHMManufacture/byId/"+id,
        type:"get",
        dataType:"json",
        success:function (date){
            $("#register").html(date.register);
            $("#productId").html(date.productId);
            $("#productName").html(date.productName);
            $("#productDescribe").html(date.productDescribe);
            $("#amount").html(date.amount);
            $("#remark").html(date.remark);
            $("#checkTime").html(date.checkTime);
            $("#registerTime").html(date.registerTime);
            $("#realModuleCostPriceSum").html(date.realModuleCostPriceSum);
            $("#moduleCostPriceSum").html(date.moduleCostPriceSum);
            $("#checker").html(date.checker);
            $("#realLabourCostPriceSum").html(date.realLabourCostPriceSum);
            $("#labourCostPriceSum").html(date.labourCostPriceSum);
            $(".manufactureId").html(date.manufactureId);
            let data=date.mProcedures;
            let tr="";
            for (let i = 0; i < data.length; i++) {
                tr+="<tr>\n" +
                    "                    <td>"+data[i].procedureName+"</td>\n" +
                    "                    <td>"+data[i].labourHourAmount+"</td>\n" +
                    "                    <td>"+data[i].realLabourHourAmount+"</td>\n" +
                    "                    <td>"+data[i].subtotal+"</td>\n" +
                    "                    <td>"+data[i].realSubtotal+"</td>\n" +
                    "                    <td>"+data[i].moduleSubtotal+"</td>\n" +
                    "                    <td>"+data[i].realModuleSubtotal+"</td>\n" +
                    "                    <td>" ;

                if (data[i].procedureFinishTag=='0'){
                    tr+="<a href='#' onclick='dengji("+JSON.stringify(data[i])+",this,"+i+")'>登记</a>";
                }else if (data[i].procedureFinishTag=='1'){
                    tr+="完成";
                }else if (data[i].procedureFinishTag=='2'){
                    tr+="未完成";
                }else if (data[i].procedureFinishTag=='3'){
                    tr+="等待审核";
                }
                tr+="</td>";

                if (data[i].procedureTransferTag=='0' && data[i].procedureFinishTag=='1'){
                    if (i==0){
                        tr+="<td><a data-toggle=\"modal\" data-target=\"#myModal\"" +
                            " href='#' onclick='jiaojiedengji("+data[i].id+","+i+","+data[i].demandAmount+")'>交接登记</a></td>";
                    }else{
                        tr+="<td><a data-toggle=\"modal\" data-target=\"#myModal\"" +
                            " href='#' onclick='jiaojiedengji("+data[i].id+","+i+","+data[i-1].realAmount+")'>交接登记</a></td>";
                    }

                }else if (data[i].procedureTransferTag=='1' && data[i].procedureFinishTag=='1'){
                    tr+="<td>完成</td>";
                }else if (data[i].procedureTransferTag=='2' && data[i].procedureFinishTag=='1'){
                    tr+="<td>等待审核</td>";
                }else{
                    tr+="<td></td>";
                }

                tr+= "</tr>";
            }
            $("#tbodyForTr").html(tr);

        }
    })
}

//返回列表
function returnList(){
    $(".div1").show();
    $(".div2").hide();
    $(".div3").hide();
}

//点击登记
let mPId=0;
let mProceduring={};
let mProceduringModuling=[];
function dengji(dui,that,index){
    mPId=dui.id;
    let td = $(that).parent().parent().prev().find("td:eq(8)").html();
    if (td=='完成' || index=='0'){
        $(".div1").hide();
        $(".div2").hide();
        $(".div3").show();
        $("#gxmc").html(dui.procedureName);
        $("#sjgss").html(dui.labourHourAmount);
        $("#yygss").html('0.0');
        $("#bcgss").val('0.0');
        $("#fuheren").html("");
        $("#fuzeren").val('');
        $("#fuheshijian").html('');
        mProceduring=dui;
        mProceduring.procedureDescribe=mProceduring.procedureMark;
        mProceduring.register=parent.document.getElementsByClassName("user_name")[0].innerHTML;

        $.ajax({
            url:"/DHMManufacture/queryByParentIdPM",
            type:"get",
            data:{
                parentId:dui.id
            },
            dataType:"json",
            success:function (data){
                mProceduringModuling=data;
                let tr="";
                for (let i = 0; i < data.length; i++) {
                    tr+="<tr>\n" +
                        "                    <td>"+data[i].detailsNumber+"</td>\n" +
                        "                    <td>"+data[i].productName+"</td>\n" +
                        "                    <td>"+data[i].productId+"</td>\n" +
                        "                    <td>"+data[i].amount+"</td>\n" +
                        "                    <td>0</td>\n" +
                        "                    <td>0</td>\n" +
                        "                    <td><input type=\"number\"  class='form-control input-full' name='amount'></td>\n" +
                        "                    <td></td>\n" +
                        "                </tr>";
                }
                $("#tbodyModuleForTr").html(tr);
            }
        })
    }else{
        alert("请先完成上一个工序的登记");
    }

}

//点击交接登记
function jiaojiedengji(id,index,amount){
    mPId=id;
    if (index==0){
        $(".label1").show();
        $(".label2").hide();
        $(".tishi").html(amount);
    }else{
        $(".label1").hide();
        $(".label2").show();
        $(".tishi").html(amount);
    }
}

//返回工序
function returnGongXu(){
    $(".div1").hide();
    $(".div2").show();
    $(".div3").hide();
}

//点击提交登记按钮
function submitRegister(){
    mProceduring.labourHourAmount=$("#bcgss").val();
    mProceduring.subtotal=$("#bcgss").val()*mProceduring.costPrice;
    mProceduring.procedureResponsiblePerson= $("#fuzeren").val();
    let amounts = $("input[name='amount']");
    for (let i = 0; i < mProceduringModuling.length; i++) {
        mProceduringModuling[i].amount=$(amounts[i]).val();
        mProceduringModuling[i].subtotal=$(amounts[i]).val()*mProceduringModuling[i].costPrice;
    }
    mProceduring.mProcedureModulings=mProceduringModuling;
    let mManufacture={
        id:mMId,
        manufactureProcedureTag:"1",
    };
    let mProcedure={
        id: mPId,
        procedureFinishTag:"3",
    };
    if (confirm("确定已经完成了本工序了吗？")){
        $.ajax({
            url:"/DHMProceduring/insertSelectMProceduring",
            type:"post",
            contentType: "application/json;charset=utf-8",
            data:JSON.stringify(mProceduring),
            success:function (data){}
        })
        $.ajax({
            url:"/DHMManufacture/up",
            type:"put",
            contentType: "application/json;charset=utf-8",
            data:JSON.stringify(mManufacture),
            success:function (data){}
        })

        $.ajax({
            url:"/DHMManufacture/updateMProcedureById",
            type:"put",
            contentType: "application/json;charset=utf-8",
            data:JSON.stringify(mProcedure),
            success:function (data){}
        })
        $(".div1").show();
        $(".div2").hide();
        $(".div3").hide();
        $("#scdjTable").bootstrapTable("refresh");
    }
}

//确认提交交接登记
function submitYes(){
    let count = $(".tishi")[0].innerHTML;
    console.log(count)
    if(parseInt($("#realAmount").val())>parseInt(count)){
        $("#msg").html("数量不合理");
    }else if (parseInt($("#realAmount").val())<0){
        $("#msg").html("数量不能小于0")
    }else{
        let mManufacture={
            id:mMId,
            manufactureProcedureTag:"1",
        };
        let mProcedure={
            id: mPId,
            procedureTransferTag:"2",
            realAmount:$("#realAmount").val(),
        };
        $("#myModal").modal("hide");
        $.ajax({
            url:"/DHMManufacture/up",
            type:"put",
            contentType: "application/json;charset=utf-8",
            data:JSON.stringify(mManufacture),
            success:function (data){}
        })

        $.ajax({
            url:"/DHMManufacture/updateMProcedureById",
            type:"put",
            contentType: "application/json;charset=utf-8",
            data:JSON.stringify(mProcedure),
            success:function (data){
                $(".div1").show();
                $(".div2").hide();
                $(".div3").hide();
                $("#scdjTable").bootstrapTable("refresh");
            }
        })
    }
}